from multiprocessing import Semaphore, Process
import time
# 多进程要调用装饰器,需要导入functiontool.wraps包,将返回值进行序列化,否则会程序会报错
from functools import wraps
""""定义信号量同时允许几个进程实现,进程处于执行态时,信号量减一,进程执行完毕信号量加1,当信号量为0时,阻塞进程 直到由其他进程释放信号量加一才执行"""


def outer(func):
    # 多进程要调用装饰器, 需要导入functiontool.wraps包, 将返回值进行序列化, 否则会程序会报错
    @wraps(func)
    def wrapper(*args, **kwargs):
        print("start the {0:*^10}".format(func.__name__))
        time.sleep(4)
        res = func(*args, **kwargs)
        print("end the {0:*^10}".format(func.__name__))
        return res

    return wrapper


@outer
def func1(sig):
    sig.acquire()
    print("hell world")
    sig.release()


@outer
def func2(sig):
    sig.acquire()
    print("hello python")
    sig.release()


if __name__ == "__main__":
    sig = Semaphore(2)
    p1 = Process(target=func1, args=(sig,))
    p2 = Process(target=func2, args=(sig,))
    p1.start()
    p2.start()
    p1.join()
    p2.join()